package com.zretc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zretc.dao.UsersDao;
import com.zretc.dao.impl.UsersDaoImpl;
import com.zretc.entity.Admin;
import com.zretc.entity.Users;
import com.zretc.service.UsersService;
import com.zretc.service.impl.UsersServiceImpl;
import com.zretc.tools.MD5Util;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// UsersServlet要调用service所以这里需要创建
	// UsersService对象
	UsersDao usersDao =new UsersDaoImpl();
	UsersService us = new UsersServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1设置请求参数
		//request.setCharacterEncoding("utf-8");

		// 2 获取op的值
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if ("".equals(op)) {
			getusers(request, response);

		}else if ("add".equals(op)) {
			addusers(request, response);
		}

	}
/**
 * 查询所有
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	private void getusers(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取前台传递过来的查询的关键字
		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 查询 将结果存储在属性范围内，转发到显示页面去

		List<Users> list = us.getUsers(keywords);
		
		request.setAttribute("list", list);
		
		//转发
		request.getRequestDispatcher("users.jsp").forward(request, response);
	}
	
	/**
	 *  管理员注册
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addusers(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		String userpwd = request.getParameter("userpwd");
		String md5Pwd = MD5Util.getEncodeByMd5(userpwd);
		String reuserpwd = request.getParameter("reuserpwd");
		System.out.println("username : "+username);
		
		if (userpwd.equals(reuserpwd)) {
			Users u=new Users(0,username, md5Pwd, 0);
			boolean flag = us.addUsers(u);
			if (flag) {
				request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
			} else {
				response.getWriter().print("<script>alert('注册失败');location.href='register.jsp'</script>");
			}
		} else {
			response.getWriter().print("<script>alert('两次输入密码不一致');location.href='register.jsp'</script>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
